/*
 * rmgr.c
 *
 * 资源管理器定义
 *
 * src/backend/access/transam/rmgr.c
 */
#include "postgres.h"

#include "access/brin_xlog.h"
#include "access/clog.h"
#include "access/commit_ts.h"
#include "access/generic_xlog.h"
#include "access/ginxlog.h"
#include "access/gistxlog.h"
#include "access/hash_xlog.h"
#include "access/heapam_xlog.h"
#include "access/multixact.h"
#include "access/nbtxlog.h"
#include "access/spgxlog.h"
#include "access/xact.h"
#include "access/xlog_internal.h"
#include "catalog/storage_xlog.h"
#include "commands/dbcommands_xlog.h"
#include "commands/sequence.h"
#include "commands/tablespace.h"
#include "fmgr.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "replication/decode.h"
#include "replication/message.h"
#include "replication/origin.h"
#ifdef FDD
#include "replication/slot_xlog.h"
#endif
#include "storage/standby.h"
#include "utils/builtins.h"
#include "utils/relmapper.h"

/* 必须与 xlog_internal.h 中的 RmgrData 定义保持同步 */
#define PG_RMGR(symname,name,redo,desc,identify,startup,cleanup,mask,decode) \
	{ name, redo, desc, identify, startup, cleanup, mask, decode },

RmgrData	RmgrTable[RM_MAX_ID + 1] = {
#include "access/rmgrlist.h"
};

/*
 * 启动所有资源管理器。
 */
void RmgrStartup(void)
{
	for (int fc_rmid = 0; fc_rmid <= RM_MAX_ID; fc_rmid++)
	{
		if (!RmgrIdExists(fc_rmid))
			continue;

		if (RmgrTable[fc_rmid].rm_startup != NULL)
			RmgrTable[fc_rmid].rm_startup();
	}
}

/*
 * 清理所有资源管理器。
 */
void RmgrCleanup(void)
{
	for (int fc_rmid = 0; fc_rmid <= RM_MAX_ID; fc_rmid++)
	{
		if (!RmgrIdExists(fc_rmid))
			continue;

		if (RmgrTable[fc_rmid].rm_cleanup != NULL)
			RmgrTable[fc_rmid].rm_cleanup();
	}
}

/*
 * 当我们遇到一个我们不认识的 RmgrId 的记录时，发出错误。
 */
void RmgrNotFound(RmgrId fc_rmid)
{
	ereport(ERROR, (errmsg("resource manager with ID %d not registered", fc_rmid),
					errhint("Include the extension module that implements this resource manager in shared_preload_libraries.")));
}

/*
 * 注册一个新的自定义 WAL 资源管理器。
 *
 * 资源管理器 ID 必须在所有扩展中全局唯一。请参阅 
 * https://wiki.postgresql.org/wiki/CustomWALResourceManagers 以保留 
 * 您扩展的唯一 RmgrId，以避免与其他扩展开发者发生冲突。开发期间，使用 RM_EXPERIMENTAL_ID 以避免不必要的 
 * 保留新 ID 的需求。
 */
void RegisterCustomRmgr(RmgrId fc_rmid, RmgrData *fc_rmgr)
{
	if (fc_rmgr->rm_name == NULL || strlen(fc_rmgr->rm_name) == 0)
		ereport(ERROR, (errmsg("custom resource manager name is invalid"),
						errhint("Provide a non-empty name for the custom resource manager.")));

	if (!RmgrIdIsCustom(fc_rmid))
		ereport(ERROR, (errmsg("custom resource manager ID %d is out of range", fc_rmid),
						errhint("Provide a custom resource manager ID between %d and %d.",
								RM_MIN_CUSTOM_ID, RM_MAX_CUSTOM_ID)));

	if (!process_shared_preload_libraries_in_progress)
		ereport(ERROR,
				(errmsg("failed to register custom resource manager \"%s\" with ID %d", fc_rmgr->rm_name, fc_rmid),
				 errdetail("Custom resource manager must be registered while initializing modules in shared_preload_libraries.")));

	if (RmgrTable[fc_rmid].rm_name != NULL)
		ereport(ERROR,
				(errmsg("failed to register custom resource manager \"%s\" with ID %d", fc_rmgr->rm_name, fc_rmid),
				 errdetail("Custom resource manager \"%s\" already registered with the same ID.",
						   RmgrTable[fc_rmid].rm_name)));

	/* 检查是否存在同名的 rmgr */
	for (int fc_existing_rmid = 0; fc_existing_rmid <= RM_MAX_ID; fc_existing_rmid++)
	{
		if (!RmgrIdExists(fc_existing_rmid))
			continue;

		if (!pg_strcasecmp(RmgrTable[fc_existing_rmid].rm_name, fc_rmgr->rm_name))
			ereport(ERROR,
					(errmsg("failed to register custom resource manager \"%s\" with ID %d", fc_rmgr->rm_name, fc_rmid),
					 errdetail("Existing resource manager with ID %d has the same name.", fc_existing_rmid)));
	}

	/* 注册它 */
	RmgrTable[fc_rmid] = *fc_rmgr;
	ereport(LOG,
			(errmsg("registered custom resource manager \"%s\" with ID %d",
					fc_rmgr->rm_name, fc_rmid)));
}

/* 显示加载的资源管理器的 SQL SRF */
Datum pg_get_wal_resource_managers(PG_FUNCTION_ARGS)
{
#define PG_GET_RESOURCE_MANAGERS_COLS 3
	ReturnSetInfo *fc_rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
	Datum		fc_values[PG_GET_RESOURCE_MANAGERS_COLS];
	bool		fc_nulls[PG_GET_RESOURCE_MANAGERS_COLS] = {0};

	InitMaterializedSRF(fcinfo, 0);

	for (int fc_rmid = 0; fc_rmid <= RM_MAX_ID; fc_rmid++)
	{
		if (!RmgrIdExists(fc_rmid))
			continue;
		fc_values[0] = Int32GetDatum(fc_rmid);
		fc_values[1] = CStringGetTextDatum(GetRmgr(fc_rmid).rm_name);
		fc_values[2] = BoolGetDatum(RmgrIdIsBuiltin(fc_rmid));
		tuplestore_putvalues(fc_rsinfo->setResult, fc_rsinfo->setDesc, fc_values, fc_nulls);
	}

	return (Datum) 0;
}
